package com.cskaoyan.servlet;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdService;
import com.cskaoyan.service.AdServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.URIUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Oarin
 * @class AdServlet
 * @date 2023/6/1 10:37
 */
@WebServlet("/admin/ad/*")
public class AdServlet extends HttpServlet {
    private AdService adService = new AdServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(),request);
        switch (operation) {
            case "/list":
                list(request, response);
                break;
            case "/create":
                create(request, response);
                break;
            case "/update":
                update(request, response);
                break;
            case "/delete":
                delete(request, response);
                break;
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        String name = StringUtil.isEmpty(request.getParameter("name")) ? null : request.getParameter("name");
        String content = StringUtil.isEmpty(request.getParameter("content")) ? null : request.getParameter("content");;

        List<MarketAd> marketAds = adService.queryAdList(name, content, page, limit, sort, order);
        ResponseUtil.responseJson(response, BaseRespVo.ok(CommonData.data(marketAds)));

    }

    private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String reqJsonStr = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(reqJsonStr, MarketAd.class);
        MarketAd createdMarketAd = adService.create(marketAd);
        ResponseUtil.responseJson(response, BaseRespVo.ok(createdMarketAd));

    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String reqJsonStr = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(reqJsonStr, MarketAd.class);
        MarketAd updatedMarketAd = adService.update(marketAd);
        ResponseUtil.responseJson(response, BaseRespVo.ok(updatedMarketAd));
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String reqJsonStr = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(reqJsonStr, MarketAd.class);
        int delete = adService.delete(marketAd);
        if (delete == 1) {
            ResponseUtil.responseJson(response, BaseRespVo.ok());
        } else {
            ResponseUtil.responseJson(response, BaseRespVo.fail("delete失败"));
        }

    }
}
